package com.example.shop.user.aop;

import com.example.shop.common.api.ResponseResult;
import com.example.shop.common.api.ServiceStateCode;
import com.example.shop.common.enums.SessionAttributeKey;
import com.example.shop.pojo.User;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;

//@Component
//@Aspect
public class UserControllerAop {
//    @Pointcut(value = "bean(userController) && ( execution(* *bind*(..)) || execution(* update*(..)) )")
//    public void loginAfterFunctions(){
//    }

    @Around(value = "bean(userController) && @annotation(Aop)")
    public Object aroundLoginAfterAbleOption(ProceedingJoinPoint joinPoint){

        Object returnValue = null;
        try {
            //按方法的参数顺序
            Object[] args = joinPoint.getArgs();
            HttpSession session = (HttpSession) args[0];
            if(session.getAttribute(SessionAttributeKey.NOW_LOGIN_USER.getKey()) == null){
                return ResponseResult.auto(ServiceStateCode.LOGIN_AFTER_OPERATION,null);
            }else{
                returnValue = joinPoint.proceed();
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseResult.failure(ServiceStateCode.FAILURE);
        }
        return returnValue;
    }
}
